import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { watermarkApi, type HistoryRecord } from '../api/watermark'

export function useHistory() {
  const history = ref<HistoryRecord[]>([])
  const loading = ref(false)

  // 获取处理历史
  const fetchHistory = async () => {
    loading.value = true
    try {
      const response = await watermarkApi.getHistory()
      history.value = response.history || []
    } catch (error: any) {
      ElMessage.error('获取历史记录失败: ' + (error.message || '未知错误'))
      console.error('获取历史记录失败:', error)
    } finally {
      loading.value = false
    }
  }

  // 删除记录
  const deleteRecord = async (id: string) => {
    loading.value = true
    try {
      await watermarkApi.deleteRecord(id)
      ElMessage.success('记录删除成功')
      // 从本地列表中删除
      history.value = history.value.filter(item => item.id !== id)
    } catch (error: any) {
      ElMessage.error('删除记录失败: ' + (error.message || '未知错误'))
      console.error('删除记录失败:', error)
    } finally {
      loading.value = false
    }
  }

  return {
    history,
    loading,
    fetchHistory,
    deleteRecord
  }
} 